package com.revzone.demo001.config;

import com.revzone.demo001.shiro.UserRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig_old {
    /**
     * 先走 filter ，然后 filter 如果检测到请求头存在 token，则用 token 去 login，走 Realm 去验证
     */

//    @Bean
//    public UserRealm userRealm() {
//        return new UserRealm();
//    }
    // 配置密码编码器
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
//
//    @Bean
//    public SecurityManager securityManager() {
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setRealm(userRealm());
//        return securityManager;
//    }
//
//    @Bean
//    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
//        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        shiroFilterFactoryBean.setSecurityManager(securityManager);
//        // anon: 无需认证即可访问
//        // authc: 必须认证才能访问
//        // roles: 拥有某个角色才能访问 (这里主要通过注解实现，但也可以在这里配置)
//        // 添加自己的过滤器并且取名为jwt
//        Map<String, Filter> filterMap = new LinkedHashMap<String,Filter>();
//        filterMap.put("jwt", new JWTFilter());//设置我们自定义的JWT过滤器
//        shiroFilterFactoryBean.setFilters(filterMap);
//
//        // 登录页面
//        shiroFilterFactoryBean.setLoginUrl("/user/login");
//        // 登录成功后跳转页面
//        shiroFilterFactoryBean.setSuccessUrl("/index");
//        // 未授权页面
//        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
//
//        // 拦截器链定义
//        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
////        filterChainDefinitionMap.put("jwt", new JWTFilter());//设置我们自定义的JWT过滤器
//        // 配置具体的拦截规则
//         filterChainDefinitionMap.put("/**", "jwt"); // 示例：所有用户需要认证
//         filterChainDefinitionMap.put("/user/login", "anon"); // 示例：所有用户需要认证
////         filterChainDefinitionMap.put("/oto/user/update", "authc,roles[admin]"); // 示例：添加用户需要admin角色
//
//        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
//        return shiroFilterFactoryBean;
//    }
//
//    /**
//     * 开启 Shiro 注解支持
//     */
//    @Bean
//    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
//        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
//        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
//        return authorizationAttributeSourceAdvisor;
//    }
}
